package org.eden.web.controller.blog;

import org.eden.rpc.common.entity.dao.Tag;
import org.eden.rpc.common.exception.ConnectException;
import org.eden.rpc.common.service.blog.TagService;
import org.eden.rpc.core.client.pooled.DefaultRpcClientPool;
import org.eden.web.pojo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/blog")
public class TagController {

    @Autowired
    DefaultRpcClientPool clientPool;

    @PostMapping("/tag")
    @PreAuthorize("hasAuthority('admin')")
    public Response addTag(Tag tag) {
        try {
            TagService service = clientPool.getService(TagService.class);
            Long id = service.addTag(tag);
            clientPool.retrieve();
            return Response.success(id);
        } catch (ConnectException e) {
            return Response.failed(e.getMessage());
        }
    }

    @GetMapping("/tag/{id}")
    public Response getTag(@PathVariable Long id) {
        try {
            TagService service = clientPool.getService(TagService.class);
            Tag tag = service.getTag(id);
            clientPool.retrieve();
            return Response.success(tag);
        } catch (ConnectException e) {
            return Response.failed(e.getMessage());
        }
    }

}
